using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using AbstraX.BuildWizard;
using AbstraX.Contracts;

namespace AbstraX
{
    public class WizardContainer : IWizardContainer
    {
        private bool isLoaded;
        private IModelViewModel modelViewModel;
        private IWizardContainerWindow containerWindow;
        private List<IWizardFrame> frames;
        private int index;
        private int addedIndex; 
        private IBuildProxy buildProxy;
        public event OnDebugInfoHandler OnDebugInfo;

        public WizardContainer(IModelViewModel modelViewModel)
        {
            this.modelViewModel = modelViewModel;
            frames = new List<IWizardFrame>();
        }

        public int Index
        {
            get 
            { 
                return index; 
            }
            
            set 
            { 
                index = value; 
            }
        }

        public void Load(IBuildProxy buildProxy)
        {
            isLoaded = true;
            this.buildProxy = buildProxy;

            modelViewModel.LoadWizardWindow(this);
        }

        public void EnableNext()
        {
            containerWindow.EnableNext();
        }

        public bool IsLoaded
        {
            get
            {
                return isLoaded;
            }
        }

        public IWizardContainerWindow ContainerWindow
        {
            set 
            {
                containerWindow = value;

#if SILVERLIGHT
                containerWindow.OnWizardClosed += (sender, e) =>
                {
                    isLoaded = false;
                };
#endif
                if (addedIndex < this.Count)
                {
                    for (var x = addedIndex; x < this.Count; x++)
                    {
                        containerWindow.AddWizardFrame(this[x]);
                    }
                }
            }
        }

        public int IndexOf(IWizardFrame frame)
        {
            return frames.IndexOf(frame);
        }

        public void Insert(int index, IWizardFrame frame)
        {
            frame.OnDebugInfo += (sender, debugInfo) =>
            {
                OnDebugInfo(sender, debugInfo);
            };

            frames.Insert(index, frame);
        }

        public void RemoveAt(int index)
        {
            frames.RemoveAt(index);
        }

        public IWizardFrame this[int index]
        {
            get
            {
                return frames[index];
            }
            set
            {
                frames[index] = value;
            }
        }

        public void Add(IWizardFrame frame)
        {
            frame.OnDebugInfo += (sender, debugInfo) =>
            {
                OnDebugInfo(sender, debugInfo);
            };

            frames.Add(frame);

            if (containerWindow != null)
            {
                containerWindow.AddWizardFrame(frame);
                addedIndex++;
            }
        }

        public void Clear()
        {
            frames.Clear();
        }

        public bool Contains(IWizardFrame frame)
        {
            return frames.Contains(frame);
        }

        public void CopyTo(IWizardFrame[] array, int arrayIndex)
        {
            frames.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get 
            {
                return frames.Count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public bool Remove(IWizardFrame frame)
        {
            return frames.Remove(frame);
        }

        public IEnumerator<IWizardFrame> GetEnumerator()
        {
            return frames.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return frames.GetEnumerator();
        }

        public void MoveNext(string lastFrameUrl)
        {
            var buildOp = buildProxy.MoveNext(lastFrameUrl);

            buildOp.Completed += (sender, e) =>
            {
                var lastFrame = e.ReturnValue;
            };
        } 

        public BuildOperation<string> GetProperty(string UIComponentURL, string propertyName)
        {
            var buildOp = buildProxy.GetProperty(UIComponentURL, propertyName);

            return buildOp;
        }

        public AbstraX.BuildWizard.BuildInvokeOperation SetProperty(string UIComponentURL, string propertyName, string value)
        {
            var buildOp = buildProxy.SetProperty(UIComponentURL, propertyName, value);

            return buildOp;
        }

        public void Finish(string lastUIComponentURL)
        {
        }
    }
}
